package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.dto.ClassroomDto;
import com.nogroup.springbootproject.entity.Classroom;
import com.nogroup.springbootproject.dao.ClassroomDao;
import com.nogroup.springbootproject.service.ClassroomService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Classroom)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:32
 */
@Service("classroomService")
@Transactional(rollbackFor = Exception.class)
public class ClassroomServiceImpl implements ClassroomService {
    @Resource
    private ClassroomDao classroomDao;

    /**
     * 通过ID查询单条数据
     *
     * @param classroomCode 主键
     * @return 实例对象
     */
    @Override
    public Classroom queryById(String classroomCode) {
        return this.classroomDao.queryById(classroomCode);
    }

    /**
     * 分页查询
     *
     * @param classroom 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Classroom> queryByPage(Classroom classroom, PageRequest pageRequest) {
        long total = this.classroomDao.count(classroom);
        return new PageImpl<>(this.classroomDao.queryAllByLimit(classroom, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param classroom 实例对象
     * @return 实例对象
     */
    @Override
    public Classroom insert(Classroom classroom) {
        this.classroomDao.insert(classroom);
        return classroom;
    }

    /**
     * 修改数据
     *
     * @param classroom 实例对象
     * @return 实例对象
     */
    @Override
    public Classroom update(Classroom classroom) {
        this.classroomDao.update(classroom);
        return this.queryById(classroom.getClassroomCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param classroom 主键
     * @return 是否成功
     */
    @Override
    public Classroom deleteById(Classroom classroom) {
        this.classroomDao.deleteById(classroom);
        return classroom;
    }

    @Override
    public List<Classroom> getAllClassrooms() {
        return classroomDao.getAllClassrooms();
    }

    @Override
    public List<Classroom> getClassroomsByFuzzySearch(ClassroomDto classroomDto) {
        return classroomDao.getClassroomsByFuzzySearch(classroomDto);
    }

    @Override
    public Long countPage(ClassroomDto classroomDto) {
        return classroomDao.CountPage(classroomDto);
    }
}
